using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BlueGameComponent
{
    class ParallaxingLayer
    {
        Texture2D Texture;
        float Speed;
        float Pos;
        int ScreenRange;
        bool Vertical;

        public ParallaxingLayer(Texture2D Texture, float Speed, bool Vertical, int ScreenRange)
        {
            this.Texture = Texture;
            this.Speed = Speed;
            this.ScreenRange = ScreenRange;
            this.Vertical = Vertical;
            Pos = 0;
        }
        public void Update()
        {
            Pos += Speed;
            int Offset = Vertical ? Texture.Width : Texture.Height;
            if (Pos < -Offset)
            {
                Pos = 0;
            }
            if (Pos > ScreenRange)
            {
                Pos = ScreenRange - Offset;
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            int Offset = Vertical ? Texture.Width : Texture.Height;
            Vector2 Postion = new Vector2();
            for (int i = 0; ; i++)
            {
                int TempPos = (int)Pos + Offset * i;
                if (TempPos >= ScreenRange)
                {
                    break;
                }
                
                if( Vertical )
                {
                    Postion.X = TempPos;
                    Postion.Y = 0;
                }
                else
                {
                    Postion.X = 0;
                    Postion.Y = TempPos;
                }
                spriteBatch.Draw(Texture, Postion, Color.White);
            }

            if (Pos > 0)
            {
                for (int i = 0; ; i++)
                {
                    int TempPos = (int)Pos - Offset * (i + 1);
                    if (Vertical)
                    {
                        Postion.X = TempPos;
                        Postion.Y = 0;
                    }
                    else
                    {
                        Postion.X = 0;
                        Postion.Y = TempPos;
                    }
                    spriteBatch.Draw(Texture, Postion, Color.White);
                    if (TempPos <= 0)
                    {
                        break;
                    }
                }
            }
        }
    }

    public class ParallaxingBackground
    {
        IList<ParallaxingLayer> List;
        bool Vertical;
        int ScreenRange;

        public void Initialize(bool Vertical, int ScreenRange)
        {
            List = new List<ParallaxingLayer>(0);
            this.Vertical = Vertical;
            this.ScreenRange = ScreenRange;
        }
        public void AddLayer(Texture2D Texture, float Speed)
        {
            ParallaxingLayer Layer = new ParallaxingLayer(Texture, Speed, Vertical, ScreenRange);
            List.Add(Layer);
        }
        public void Update()
        {
            foreach (ParallaxingLayer Layer in List)
            {
                Layer.Update();
            }
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (ParallaxingLayer Layer in List)
            {
                Layer.Draw(spriteBatch);
            }
        }
    }
}
